package com.nuoyuan.wx;

import com.nuoyuan.wx.common.utils.DateUtil;
import com.nuoyuan.wx.common.utils.HashUtil;
import com.nuoyuan.wx.common.utils.HttpClientUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @Package com.crm.admin.management
 * @ClassName SifangPayTest
 * @Description TODO
 * @Author linus
 * @Date 2020-02-26 21:01
 * @Version 1.0
 **/
public class SifangPayTest {

    public static void main(String[] args) throws Exception {
        String AuthorizationURL = "http://47.104.23.131/Pay_Index.html";
        String keyValue = "2bs2i0lgv0rfzsehuvmj3q4kpyaxudov";
        String pay_bankcode = "923";
        String pay_memberid = "200296151";//商户id
        String pay_orderid = generateOrderId();//20位订单号 时间戳+6位随机字符串组成
        String pay_applydate = generateTime();//yyyy-MM-dd HH:mm:ss
        String pay_notifyurl = "http://47.104.23.130/Pay/notifyurl.asp";//通知地址
        String pay_callbackurl = "http://47.104.23.130/Pay/Callback.asp";//回调地址
        String pay_amount = "300";
//        String pay_attach = "";
        String pay_productname = "100元话费充值";
//        String pay_productnum = "";
//        String pay_productdesc = "";
//        String pay_producturl = "";
        String stringSignTemp = "pay_amount=" + pay_amount +
                "&pay_applydate=" + pay_applydate +
                "&pay_bankcode=" + pay_bankcode +
                "&pay_callbackurl=" + pay_callbackurl +
                "&pay_memberid=" + pay_memberid +
                "&pay_notifyurl=" + pay_notifyurl +
                "&pay_orderid=" + pay_orderid +
                "&key=" + keyValue;

        String pay_md5sign = HashUtil.getStringMD5(stringSignTemp);

        Map<String, String> params = new HashMap<>();
        params.put("pay_memberid", pay_memberid);
        params.put("pay_orderid", pay_orderid);
        params.put("pay_applydate", pay_applydate);
        params.put("pay_bankcode", pay_bankcode);
        params.put("pay_notifyurl", pay_notifyurl);
        params.put("pay_callbackurl", pay_callbackurl);
        params.put("pay_amount", pay_amount);
        params.put("pay_md5sign", pay_md5sign.toUpperCase());
        params.put("pay_productname", pay_productname);

        String result = HttpClientUtils.executePost(AuthorizationURL, params);
        System.out.println(result);
    }


    public static String generateOrderId() {
        String keyup_prefix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(DateUtil.getCurrentTimestamp().getTime()));
        String keyup_append = String.valueOf(new Random().nextInt(899999) + 100000);
        String pay_orderid = keyup_prefix + keyup_append;//订单号
        return pay_orderid;
    }

    public static String generateTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(DateUtil.getCurrentTimestamp().getTime()));
    }

    public static String md5(String str) throws NoSuchAlgorithmException {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte[] byteDigest = md.digest();
            int i;
            //字符数组转换成字符串
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < byteDigest.length; offset++) {
                i = byteDigest[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                    buf.append(Integer.toHexString(i));
                }
            }
            // 32位加密
            return buf.toString().toUpperCase();
            // 16位的加密
            //return buf.toString().substring(8, 24).toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
}
